package com.nxxskj.common.intercepter;

import com.nxxskj.alcohol.bean.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class SessionInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        // 从session中获取用户信息
        User user = (User) session.getAttribute("user");
        // session过期
        if (user == null) {
            response.sendRedirect(request.getContextPath() + "/login.do");
            return false;
        } else {
            return true;
        }

    }
}
